package mysql

import (
	"goods/goodsrpc/goodsrpc"
	"gorm.io/gorm"
)

type Good struct {
	gorm.Model
	Name    string  `gorm:"type:varchar(20);not null;comment:商品名称"`
	Price   float64 `gorm:"type:decimal(10);not null;comment:商品价格"`
	Stock   int64   `gorm:"type:int;not null;comment:商品库存"`
	Image   string  `gorm:"type:varchar(500);not null;comment:商品图片"`
	ClassId int64   `gorm:"type:int;not null;comment:分类id"`
}

func (g *Good) GoodAdd(db *gorm.DB) error {
	return db.Create(&g).Error
}

func (g *Good) FindGoods(db *gorm.DB, name string) error {
	return db.Where("name=?", name).First(&g).Error
}

func GoodsList(db *gorm.DB, list []*goodsrpc.GoodList) []*goodsrpc.GoodList {
	db.Debug().Model(&Good{}).
		Select("goods.id as goods_id",
			"goods.name as goods_name",
			"goods.price",
			"goods.stock",
			"goods.image",
			"classes.name as class_name",
			"goods.created_at").
		Joins("left join classes on classes.id=goods.class_id").
		Find(&list)
	return list
}
